package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorsMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import jdk.jshell.Snippet;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.bridge.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DishServiceImp implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorsMapper flavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;



    /**
     * 新增菜品和口味
     * @param dishDTO
     */
    @Transactional
    @Override
    public void dishWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //插入一条菜品
        dishMapper.insert(dish);

        //获取插入菜品的主键id
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0)
        {
            //遍历集合 设置dishId
            flavors.forEach(dishFlavor ->{
                dishFlavor.setDishId(dishId);
            });

            //插入n条口味
            flavorsMapper.insert(flavors);
        }


    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //分页查询统一返回page对象
        Page<DishVO> dishVO=  dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(dishVO.getTotal(),dishVO.getResult());
    }

    @Override
    public void deleteById(List<Long> ids) {
        //如果菜品处于起售中，则不能删除，抛出异常
        for (Long id : ids) {
            Dish dish =  dishMapper.searchBID(id);
            System.out.println(dish);
            if(dish.getStatus() == StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //被套餐关联的菜品不能删除
       List<Long> setmealIds = setmealDishMapper.getSetmealDishIds(ids);
        if(setmealIds!=null && setmealIds.size()>0)
        {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //根据id批量删除菜品
        dishMapper.deleteBID(ids);
        flavorsMapper.deleteByDishId(ids);
    }

    /**
     * 根据id查询菜品及对应的口味
     * @param id
     * @return
     */
    @Override
    public DishVO getBIdWithFlavors(Long id) {
        //根据id查询菜品
        Dish dish = dishMapper.searchBID(id);
        List<DishFlavor> dishFlavors =  flavorsMapper.searchBID(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updateWithFlavors(DishDTO dishDTO) {
        //修改菜品对应的字段
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        //修改对应的口味 先删除所有口味在新增
        flavorsMapper.deleteByDishId(Collections.singletonList(dish.getId()));
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0)
        {
            //遍历集合 设置dishId
            flavors.forEach(dishFlavor ->{
                dishFlavor.setDishId(dishDTO.getId());  //前端传过来的dishID不是非必须的，所以需要设置
            });

            //插入n条口味
            flavorsMapper.insert(flavors);
        }

    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorsMapper.searchBID(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
